//index.js
const app = getApp();
const items = require('../../data/index');
Page({
  data: {
    // 歌曲列表
    items: items,

    // 当前下标
    current:0,
    // 当前对象
    item: null,
    // 状态控制
    playing: false,
    // 是否关闭
    stoped:false,
    // 总共时间
    totalTime:0,
    // 当前时间
    currentTime:0,
    // 状态栏高度
    statusBarHeight:app.globalData.statusBarHeight,
    // 标题栏高度
    titleBarHeight:app.globalData.titleBarHeight,
  },

  // 加载完成事件
  onLoad() {
    this.loadMusic(0);
  },

  // 轮播改变事件
  handleSwiperChange(e) {
    const current = e.detail.current;
    this.setData({current:current},()=>{
      this.loadMusic(this.data.current);
    });
  },

  // 播放按钮点击事件
  handlePlay() {
    wx.vibrateShort();
    const manager = wx.getBackgroundAudioManager();

    // 如果正在播放，则进行暂停逻辑
    if(this.data.playing) {
      this.setData({playing:false});
      manager.pause();
      return;
    }
    // 如果是暂停，则进行播放逻辑
    if(!this.data.playing && !this.data.stoped) {
      this.setData({playing:true});
      manager.play();
      return;
    }
    // 如果是关闭过，则重放此音乐
    if(!this.data.playing && this.data.stoped) {
      this.setData({playing:true,stoped:false});
      manager.protocol = 'http';
      manager.title = this.data.item.name;
      manager.epname = this.data.item.name;
      manager.singer = 'ASMR';
      manager.coverImgUrl = this.data.item.logo;
      manager.src = this.data.item.url;
      return;
    }
  },

  // 进度跳转
  handleSeek(e) {
    wx.vibrateShort();
    const query = wx.createSelectorQuery();
    query.select('#process').boundingClientRect(rect=>{
      const manager = wx.getBackgroundAudioManager();
      // 如果当前已经停止音乐了，则重新开始播放音乐后，再进行跳转
      if(!this.data.playing && this.data.stoped) {
        this.setData({playing:true,stoped:false});
        manager.protocol = 'http';
        manager.title = this.data.item.name;
        manager.epname = this.data.item.name;
        manager.singer = 'ASMR';
        manager.coverImgUrl = this.data.item.logo;
        manager.src = this.data.item.url;
      }

      // 进度栏基本长度和用户点击到的位置长度
      const width = rect.width;
      const userWidth = e.detail.x - e.target.offsetLeft;
      
      // 计算所在位置百分比
      const rate = userWidth/width;
      const seek = this.data.totalTime * rate;
      
      manager.seek(seek);
    }).exec();
  },

  // 加载音乐
  loadMusic(index) {
    if(this.data.items[index]) {
      setTimeout(()=>{
        this.setData({item:this.data.items[index],playing:true,stoped:false,currentTime:0,totalTime:0},()=>{
          // 初始化音乐播放器
          const manager = wx.getBackgroundAudioManager();
          manager.protocol = 'http';
          manager.title = this.data.item.name;
          manager.epname = this.data.item.name;
          manager.singer = 'ASMR';
          manager.coverImgUrl = this.data.item.logo;
          manager.src = this.data.item.url;
  
          // 监听主动停止音乐事件
          manager.onStop(()=>{
            this.setData({playing:false,currentTime:0,stoped:true});
          });
          // 播放自然停止事件
          manager.onEnded(()=>{
            this.setData({playing:false,stoped:true,currentTime:this.data.totalTime});
          });
          // 监听背景播放进度事件
          manager.onTimeUpdate(()=>{
            this.setData({currentTime:manager.currentTime});
          });
  
          // 初始化总时长信息
          this.setData({totalTime:this.data.item.time});
        });
      },500);
      
    }
  },

  // 上一个
  handleLast() {
    if(this.data.current > 0){
      wx.vibrateShort();
      this.setData({current:this.data.current - 1});
    }
  },
  // 下一个
  handleNext() {
    if(this.data.current < (this.data.items.length - 1)){
      wx.vibrateShort();
      this.setData({current:this.data.current + 1});
    }
  },
  // 开启分享
  onShareAppMessage() {}
})